<template>
  <div style="width: 100%">
    <div id="deawer">
      <v-tableTitle
        :btnConfig="btnConfig"
        @cancel="cancel"
        @save="save"
      ></v-tableTitle>
      <!-- 主表明细 -->
      <div class="detailBox">
        <v-detail ref="detail"></v-detail>
      </div>
      <!-- 主表明细 -->
      <div class="detailListBox">
        <v-detailList ref="detailList" @subUpdateNewFlag="updateNewFlag"></v-detailList>
      </div>
    </div>
  </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";
import config from "../Config"
import tableTitle from "commons/TableTitle";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"

export default {
  name: "",
  mixins: [baseMixin, skipMixin, config],
  data () {
    return {
      newFlag: false,
      routeData: {},
      screenid: ""
    };
  },
  computed: {
    btnConfig () {
      return [
        { label: this.$t('common.save'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM03100203'), invalid: "save" },
        { label: this.$t('common.return'),certigier: !this.btnPermNameList.includes('WM03100204'), invalid: "cancel" },
      ]
    }
  },
  created () {
  },
  methods: {
    updateNewFlag(type) {
      this.newFlag = type;
    },
    //保存按钮
    save() {
      this.$refs.detail.$children[0].$refs.input.$refs["from"].validate(async (valid) => {
        if (valid) {
          let data = {}
          let detail = this.$refs.detail.detailData;
          if (this.$refs.detailList.detailFlag) {
            let datailData = this.$refs.detailList.detailData;
            let detailFlag = this.$refs.detailList.$children[1].$refs.input.submitForm("from");
            if (!detailFlag) {
              return;
            }
            data = {
              ...detail,
              detail: [
                {
                  ...datailData,
                  userkey: detail.userkey
                }
              ]
            }
          } else {
            data = {
              ...detail
            }
          }
          let url = ''
          if (this.newFlag) {
            url = '/saveTaskmanageruser'
          } else {
              url = '/updateTaskmanageruser'
          }
          try {
            let res = await this.$api.post(url, data);
            
              this.$message.success(res.returnMgr);
              if (this.newFlag) {
                this.cancel();
              } else {
                this.$refs.detailList.detailNav = "1";
              }
            
          } catch (e) {
            this.$message.error(e)
          }
        }
      })
    },
    //弹框取消
    cancel() {
      this.$router.push({name: "taskmanageruser"});
    },
  },
  mounted () {
    this.newFlag = this.$store.state.newFlag
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.routeData = data.routeData;
      this.newFlag = data.newFlag;
    } else if (!this.newFlag) {
      this.routeData = this.$store.state.routeData
    }

  },
  watch: {},
  components: {
    "v-tableTitle": tableTitle,
    "v-detail": detail,
    "v-detailList": detailList,
  }
}
</script>

<style scoped>
</style>
